package com.epalmpay.service.bonus.impl;

import com.epalmpay.exception.ApplicationException;
import com.epalmpay.dto.bonus.*;
import com.epalmpay.entity.*;
import com.epalmpay.enumdef.ActivityEnum;
import com.epalmpay.mapper.BonusAdjustBatchMapper;
import com.epalmpay.mapper.BonusSendLogMapper;
import com.epalmpay.mapper.BonusVerifyLogMapper;
import com.epalmpay.mapper.MemberMapper;
import com.epalmpay.service.bonus.IBonusAdjustBatchService;
import com.epalmpay.service.bonus.IBonusService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by gwn on 2017/9/12.
 */
@Service("bonusAdjustBatchService")
public class BonusAdjustBatchServiceImpl implements IBonusAdjustBatchService{

    @Resource
    private BonusAdjustBatchMapper bonusAdjustBatchMapper;
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private BonusSendLogMapper bonusSendLogMapper;

    @Resource
    private BonusVerifyLogMapper bonusVerifyLogMapper;

    @Resource
    private IBonusService bonusService;



    @Override
    public BonusAdjustBatchOutDTO saveAdjustAmount(BonusAdjustbatchInDTO bonusAdjustbatchInDTO) throws Exception {
        BonusAdjustBatchOutDTO bonusAdjustBatchOutDTO = new BonusAdjustBatchOutDTO();

        String[] arr = bonusAdjustbatchInDTO.getCardOrPhone().split(",");
        bonusAdjustBatchOutDTO.setTotalCount(arr.length);
        BonusAdjustBatch bonusAdjustBatch = new BonusAdjustBatch();
        Member member = new Member();
        int successCount =1;
        int failCount =1;
        String ids ="";
        for(int i=0;i<arr.length;i++){
            member.setKeyWord(arr[i]);
            member.setGroupId(bonusAdjustbatchInDTO.getGroupId());

            Member members = memberMapper.queryMemberId(member);//查询会员idnull
            if(members !=null){
                 ids +=  members.getId().toString()+',';

            }else{
                throw new ApplicationException("信息为:" + arr[i] + "的会员不存在请重新填写！");
            }
        }
        ids= ids.substring(0,ids.length()-1);
        bonusAdjustBatch.setMemberIds(ids);
        bonusAdjustBatch.setGmtCreate(new Date());
        bonusAdjustBatch.setGmtModified(new Date());
        bonusAdjustBatch.setGroupId(bonusAdjustbatchInDTO.getGroupId());
        bonusAdjustBatch.setAdjustAmount(bonusAdjustbatchInDTO.getAdjustAmount());
        bonusAdjustBatch.setAdjustNum(bonusAdjustbatchInDTO.getCardOrPhone().split(",").length);
        bonusAdjustBatch.setOperator(bonusAdjustbatchInDTO.getLoginId());
        bonusAdjustBatch.setMemo(bonusAdjustbatchInDTO.getMemo());
        bonusAdjustBatch.setAdjustStatus(ActivityEnum.BonusBatchStatus.Accounting.getType());
        bonusAdjustBatchMapper.insertSelective(bonusAdjustBatch);
        bonusAdjustBatchOutDTO.setSuccessCount(bonusAdjustbatchInDTO.getCardOrPhone().split(",").length);
        return bonusAdjustBatchOutDTO;
    }


    @Override
    public BonusAdjustBatchListOutDTO queryBonusAdjustBatchList(BonusAdjustbatchInDTO bonusAdjustbatchInDTO,Page page) {
        BonusAdjustBatchListOutDTO bonusAdjustBatchListOutDTO = new BonusAdjustBatchListOutDTO();
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId",bonusAdjustbatchInDTO.getGroupId());
        param.put("adjustStatus",bonusAdjustbatchInDTO.getAdjustStatus());
        param.put("operator",bonusAdjustbatchInDTO.getOperator());
        param.put("joinStart", bonusAdjustbatchInDTO.getJoinStart());
        param.put("joinEnd",bonusAdjustbatchInDTO.getJoinEnd());


        int sumCount = bonusAdjustBatchMapper.queryBonusAdjustBatchCount(param);
        page.setTotalResult(sumCount);
        if(sumCount<=0)
            return bonusAdjustBatchListOutDTO;

        param.put("limit", page.getShowCount());
        param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
        List<BonusAdjustBatchOutDTO> bonusAdjustBatchList = bonusAdjustBatchMapper.queryBonusAdjustBatchList(param);
        bonusAdjustBatchListOutDTO.setBonusAdjustBatchOutDTOList(bonusAdjustBatchList);
        return bonusAdjustBatchListOutDTO;

    }


    @Override
    public void updateAdjustStatus(BonusAdjustBatch bonusAdjustBatch) throws Exception {
        bonusAdjustBatchMapper.updateByPrimaryKeySelective(bonusAdjustBatch);

        BonusAdjustBatch bonusAdjustBatchs =bonusAdjustBatchMapper.queryBonusAdjustBatch(bonusAdjustBatch);

        String[] arr = bonusAdjustBatchs.getMemberIds().split(",");

            Map<String, Integer> map = new HashMap<String, Integer>();
            for(int i=0;i<arr.length;i++){
                Long memberId =Long.parseLong(arr[i]);
                BonusSendLog bonusSendLog = BonusSendLog.build(bonusAdjustBatchs.getGroupId(),memberId,
                    ActivityEnum.BonusSendType.ManualAdd.getType(),bonusAdjustBatchs.getAdjustAmount(),ActivityEnum.BonusSendStatus.Accounted.getType(),bonusAdjustBatch.getId(),null,null,null);
                bonusService.sendOneMemberBonus(bonusSendLog);
//                Member members = memberMapper.queryMemberId(member);//查询会员idnull
            }


//        if(bonusAdjustBatchs.getAdjustAmount()>0){
//
//            String[] arr = bonusAdjustBatchs.getMemberIds().split(",");
//
//            Map<String, Integer> map = new HashMap<String, Integer>();
//            for(int i=0;i<arr.length;i++){
//                Long memberId =Long.parseLong(arr[i]);
//                BonusSendLog bonusSendLog = BonusSendLog.build(bonusAdjustBatchs.getGroupId(),memberId,
//                    ActivityEnum.BonusSendType.ManualAdd.getType(),bonusAdjustBatchs.getAdjustAmount(),ActivityEnum.BonusSendStatus.Accounted.getType(),bonusAdjustBatch.getId(),null,null,null);
//                bonusService.sendOneMemberBonus(bonusSendLog);
////                Member members = memberMapper.queryMemberId(member);//查询会员idnull
//            }
//
//        }else{
//
//            String[] arr1 = bonusAdjustBatchs.getMemberIds().split(",");
//            for(int i=0;i<arr1.length;i++){
//                Long memberId =Long.parseLong(arr1[i]);
//
//                BonusVerifyLog bonusVerifyLog = BonusVerifyLog.build(bonusAdjustBatchs.getGroupId(),memberId,
//                    ActivityEnum.BonusVerityType.ManualReduce.getType(),bonusAdjustBatchs.getAdjustAmount(),ActivityEnum.BonusVerityStatus.OutAccounted.getType(),bonusAdjustBatch.getId(),null,null,null);
//                bonusService.verifyOneMemberBonus(bonusVerifyLog, null);
//
//            }
//
//        }

    }

    @Override
    public void updateFailAdjustStatus(BonusAdjustBatch bonusAdjustBatch) throws Exception {
        bonusAdjustBatchMapper.updateByPrimaryKeySelective(bonusAdjustBatch);
    }

    @Override
    public BonusAdjustBatch queryAdjustAmount(BonusAdjustbatchInDTO bonusAdjustbatchInDTO) {
        return bonusAdjustBatchMapper.queryAdjustAmount(bonusAdjustbatchInDTO);
    }

    @Override
    public BonusAdjustBatchListOutDTO queryBonusMemberList(BonusAdjustbatchInDTO bonusAdjustbatchInDTO, Page page) {
        BonusAdjustBatchListOutDTO bonusAdjustBatchListOutDTO = new BonusAdjustBatchListOutDTO();
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("batchId",bonusAdjustbatchInDTO.getId());
        param.put("memberName",bonusAdjustbatchInDTO.getMemberName());
        param.put("groupId",bonusAdjustbatchInDTO.getGroupId());

        int sumCount = bonusSendLogMapper.queryBonusSendLogCount(param);
            page.setTotalResult(sumCount);
            if(sumCount<=0) {
                //这里是查看给那些会员加减积分  审核之前
                List<BonusMemberOutDTO> bonusMemberOutDTOList = new ArrayList<>();
                String[] memberIds = bonusAdjustbatchInDTO.getMemberIds().split(",");
                for(int i =0;i<memberIds.length ;i++){
                    Long id = Long.parseLong(memberIds[i]);
                    BonusMemberOutDTO bonusMemberOutDTO = memberMapper.queryMemberInfo(id);
                    bonusMemberOutDTOList.add(bonusMemberOutDTO);
                }
                bonusAdjustBatchListOutDTO.setBonusMemberOutDTOList(bonusMemberOutDTOList);
                return bonusAdjustBatchListOutDTO;
            }

            param.put("limit", page.getShowCount());
            param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
            List<BonusMemberOutDTO> bonusMemberOutDTOList = bonusSendLogMapper.queryBonusSendLogList(param);
            bonusAdjustBatchListOutDTO.setBonusMemberOutDTOList(bonusMemberOutDTOList);

        return bonusAdjustBatchListOutDTO;
    }

}
